package com.example.asus.practice;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatButton;
import android.view.View;
import android.widget.Toast;

import java.io.File;

public class MusicPlay extends AppCompatActivity {
    AppCompatButton bt_play;
    AppCompatButton bt_stop;
    private MediaPlayer mediaPlayer;
    boolean isPause = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_play);

        File file = new File("/sdcard/再见只是陌生人.mp3");
        if(file.exists()){
            mediaPlayer = MediaPlayer.create(this, Uri.parse(file.getAbsolutePath()));
        }else{
            Toast.makeText(this, "播放的音频不存在", Toast.LENGTH_SHORT).show();
            return;
        }

        bt_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mediaPlayer.isPlaying() && !isPause){//判断是否处于播放状态
                    mediaPlayer.pause();
                    isPause = true;
                }else{
                    mediaPlayer.start();
                    isPause = false;
                }
            }
        });

        bt_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

    }
}
